package com.sanxia.equipment.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.sanxia.equipment.domain.Finance;
import com.sanxia.equipment.mapper.FinanceMapper;
import com.sanxia.equipment.vo.EquipmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sanxia.equipment.mapper.EquipmentMapper;
import com.sanxia.equipment.domain.Equipment;
import com.sanxia.equipment.service.IEquipmentService;

/**
 * 设备列表Service业务层处理
 *
 * @author 贤哥
 * @date 2023-04-26
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService
{
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 查询设备列表
     *
     * @param equipmentId 设备列表主键
     * @return 设备列表
     */
    @Override
    public Equipment selectEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.selectEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 查询设备列表列表
     *
     * @param equipment 设备列表
     * @return 设备列表
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    /**
     * 新增设备列表
     *
     * @param equipment 设备列表
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        equipment.setCreateTime(DateUtils.getNowDate());
        return equipmentMapper.insertEquipment(equipment);
    }

    /**
     * 修改设备列表
     *
     * @param equipment 设备列表
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        equipment.setUpdateTime(DateUtils.getNowDate());
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 批量删除设备列表
     *
     * @param equipmentIds 需要删除的设备列表主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentIds(Long[] equipmentIds)
    {
        return equipmentMapper.deleteEquipmentByEquipmentIds(equipmentIds);
    }

    /**
     * 删除设备列表信息
     *
     * @param equipmentId 设备列表主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.deleteEquipmentByEquipmentId(equipmentId);
    }

    @Override
    public int updateAudit(EquipmentVo equipmentVo) {
        Equipment equipment = new Equipment();
        equipment.setEquipmentId(equipmentVo.getEquipmentId());
        equipment.setAuditStatus(equipmentVo.getAuditStatus());
        equipment.setAuditOpinion(equipmentVo.getAuditOpinion());
        equipment.setAuditTime(new Date());
        equipment.setAuditPerson(SecurityUtils.getUsername());
        return equipmentMapper.updateEquipment(equipment);
    }

    @Override
    public int updateapprover(EquipmentVo equipmentVo) {
        // 1 审批处理
        Equipment equipment = new Equipment();
        Equipment eq = null;
        equipment.setEquipmentId(equipmentVo.getEquipmentId());
        equipment.setAuditStatus(equipmentVo.getAuditStatus());
        equipment.setApproverTime(new Date());
        equipment.setApproverOpinion(equipmentVo.getApproverOpinion());
        equipment.setApproverPerson(SecurityUtils.getUsername());
        int num = equipmentMapper.updateEquipment(equipment);
        // 2 查询数据
        if(num == 1){
             eq = equipmentMapper.selectEquipmentByEquipmentId(equipmentVo.getEquipmentId());
        }

        // 3将值赋值给财务
        Finance finance = new Finance();
        finance.setFinanceName("购买"+eq.getEquipmentName());
        finance.setFinanceContent("审批后购买"+eq.getEquipmentName());
        finance.setFinanceMoney(eq.getPrice());
        finance.setSupplierId(eq.getSupplierId());
        finance.setFinanceStatus(0);
        finance.setAuditStatus(0);
        finance.setCreateTime(new Date());
        finance.setCreateBy("系统添加购买设备");

        return financeMapper.insertFinance(finance);
    }
    //保养
    @Override
    public int maintainEquipment(Equipment equipment) {
        return equipmentMapper.updateEquipment(equipment);
    }



    @Override
    public List<Equipment> selectEqList() {
        return equipmentMapper.selectEqList();
    }


}
